var ProductGradeModule = function () {
    var editable = function () {

        return {
            init: function () {
                var oTable = $('#modelEditable').dataTable({
                	"autoWidth":false,
                	 "lengthMenu": [
			                        [10, 15, 20, 50, -1],
			                        [10, 15, 20, 50, "All"]
			                    ],
                    "displayLength": -1,
                    "language": {
                        "lengthMenu": "每页 _MENU_ 条记录",
                        "paginate": {
                            "previous": "上一页",
                            "next": "下一页"
                        },
                        "info" : "",//"从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",    
                        "infoEmpty" : "",//记录数为0",
                        "sZeroRecords": "找不到相关数据"
                        //"sInfoEmpty": "没有数据"
                     },
                     "columnDefs": [{
									    "data": null,
									    'sortable': false,
									    "defaultContent": "<center>--</center>",
									    "targets": [0]
									},{
           						      "data": null,
           						      "sortable": false,
           						      "defaultContent": "",
           						      "targets": [$("#modelEditable tr th").length == 5?4:1]
           						    },{
									    "data": null,
									    'sortable': false,
									    "defaultContent": "<center>--</center>",
									    "targets": [3]
									}],
                     "columns": [{ "data": "grade" },
                                { "data": null },
                                { "data": "prospectiveEarnings" },
                                { "data": "remark" }
                               ],
                   "filter" : false,
                   "processing": false,
                   "serverSide": true,
                   "bPaginate": false,  //是否显示分页
                   "bLengthChange": false,  //每页显示的记录数
                   "ajaxSource": ctx + "/productGrade/query",
                   "serverMethod": "post",
                   "fnRowCallback" : function(nRow, aData, displayIndex) {
                	   nRow.children[0].id = aData.id;
                	   var s_scope = (aData.subScopeStart/10000.00)+"&nbsp;万元";
                	   var e_scope = (aData.subScopeEnd/10000.00)+"&nbsp;万元";
                	   var scope = s_scope+"&nbsp;-&nbsp;"+e_scope;
                	   $('td:eq(1)', nRow).html(scope);
                	   
                	   if($("#modelEditable tr th").length == 5){
                		   var opera = '<a class="edit fa fa-pencil-square-o fa-lg" title="编辑" href="javascript:;"></a>&nbsp;&nbsp;'+
                		   			   '<a class="delete fa fa-trash-o fa-lg" title="删除" href="javascript:;"></a>';
                		   $('td:eq(4)', nRow).html(opera);
                	   }
                       return nRow;
                   },
                   "fnServerData" : function(ajaxSource, dataSet, fnCallback) {
	                   var json = '{"productId":"'+$("#productId").val()+'"}';
	                   
                       $.ajax({
                       	"contentType":"application/json; charset=UTF-8",
                           "dataType" : 'json',
                           "type" : "POST",
                           "url" : ajaxSource,
                           "data" : json,
                           "success" : fnCallback
                       });
                   },
                });
                // 删除
                $('#modelEditable').on("click", "a.delete", function (e) {
                    e.preventDefault();
                    var obj = $(this);
                    bootbox.confirm("您确定删除这条记录?", function(result) {
                        if (result) {
                        	var nRow = obj.parents('tr')[0];
                            var id = nRow.children[0].id;
                            $.ajax({
                            	type:"post",
                                url: ctx + "/productGrade/delete",
                                data:"id=" + id,
                                dataType:"json",
                                success: function(msg){
                                	if (msg.code == 1) {
                                		bootbox.alert("删除产品档次成功！");
                                		oTable.fnDeleteRow(nRow);
                                	} else {
                                		bootbox.alert("删除产品档次失败！");
                                	}
                                },
                                error: function (msg) {
                                	bootbox.alert(msg.message);
                                }
                            });
                        }
                    });
                });
                
                var modal;
                // 编辑
                $('#modelEditable').on("click", "a.edit", (function (e) {
                    e.preventDefault();
                    var nRow = $(this).parents('tr')[0];
                    var id = nRow.children[0].id;
                    
                    $.ajax({
                    	type:"get",
                        url: ctx + "/productGrade/get",
                        data:"id="+id,
                        dataType:"json",
                        success: function(data){
                        	$("#grade_edit_id").text("编辑产品档次");
                        	$("#gradeId").val(data.id);
                        	$("#grade").val(data.grade);
                        	$("#subScopeStart").val(data.subScopeStart/10000.00);
                        	$("#subScopeEnd").val(data.subScopeEnd/10000.00);
                        	$("#prospectiveEarnings").val(data.prospectiveEarnings);
                        	$("#grade_remark").val(data.remark);
                        	modal = $('#updateModel').modal('show');
                        },
                        error: function (msg) {
                        	bootbox.alert(msg.message);
                        }
                    });
                    
                	
                    
                }));
               
                
                $('#doAddModel').click(function(){
                	$("#grade_edit_id").text("新增产品档次");
                	$("#gradeId").val("");
                	$("#grade").val("");
                	$("#subScopeStart").val("");
                	$("#subScopeEnd").val("");
                	$("#prospectiveEarnings").val("");
                	$("#grade_remark").val("");
                	modal = $('#updateModel').modal('show');
                });
                
                $('#saveModel').click(function(){
                	var json = $('form#gradeForm').serializeJSON();
                	var flag = $("#grade_edit_id").text().indexOf("新增")!=-1;
                	var subScopeStart = $("#subScopeStart").val().trim();
                	var subScopeEnd = $("#subScopeEnd").val().trim();
                	
                	//$("#grade").val().trim()==""||
                	if(subScopeStart == "" ||subScopeEnd == "" ||$("#prospectiveEarnings").val().trim()==""){
                		bootbox.alert("带*参数必填，不能为空！");
                		return;
                	}
                	
                	if(isNaN(subScopeStart)||isNaN(subScopeEnd)){
                		bootbox.alert("认购最小金额/认购最大金额为数字！");
                		return;
                	}
                		
                	if(parseFloat(subScopeStart) > parseFloat(subScopeEnd)){
                		bootbox.alert("认购最小金额不能大于认购最大金额！");
                		return;
                	}
                	
                	
                	$.ajax({
                		contentType:"application/json; charset=UTF-8",
                    	type:"post",
                        url: ctx + "/productGrade/"+(flag?"doAdd":"update"),
                        data: json,
                        dataType:"json",
                        success: function(data){
                        	if(data.code==1){
	                        	modal.modal('hide');
	                        	bootbox.alert(flag?"新增产品档次成功！":"更新产品档次成功！");
	                        	oTable.fnClearTable();
                        	}else{
    	                        	bootbox.alert(flag?"新增产品档次失败！":"更新产品档次失败！");
                        	}
                        	
                        },
                        error: function (msg) {
                        	bootbox.alert(msg.message);
                        }
                    });
                	
                });
                
            }

        };

    }();
    
    return {
        init: function () {
        	editable.init();
        }
    };
}();